﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RoadSystemTest
{
    /// <summary>
    /// Interaction logic for Car.xaml
    /// </summary>
    public partial class Car : UserControl, ICarObject
    {
        //A sávval csak az interfészen át kommunikál
        public RoadSystemTest.Point Position { get; private set; }

        public RoadSystemTest.Point GetPosition() { return new RoadSystemTest.Point(Position); }

        public int Speed { get; private set; }

        public int GetSpeed() { return Speed; }

        public string Caption
        {
            set
            {
                CarLabel.Content = value;
            }
        }
        public void SetCaption(string cap)
        {
            Caption = cap;
        }

        public void Disappear()
        {
            if (this.Opacity >= 0.1)
            {
                this.Opacity -= 0.1;
            }
            else
            {
                this.Opacity = 0;
            }
        }

        public bool IsDisappeared()
        {
            return this.Opacity == 0;
        }
        public Car(int speed = 1)
        {
            InitializeComponent();
            Position = new Point();
            Speed = speed;
            CarTranslateTransform.X = 0;// Position.X;
            CarTranslateTransform.Y = 0;// Position.Y;
        }

        public void Move(Point newPosition, double rotation)
        {
            Move(newPosition);
            if (this.CarRotateTransform.Angle != rotation)
            {
                this.CarRotateTransform.Angle = rotation;
            }
        }
        public void Move(Point newPosition)
        {
            this.Position.X = newPosition.X;
            this.Position.Y = newPosition.Y;
            //this.Margin = new Thickness(this.Position.X, this.Position.Y, this.Margin.Right, this.Margin.Bottom);
            CarTranslateTransform.X = Position.X;
            CarTranslateTransform.Y = Position.Y;
        }

        public void ResetEvents()
        {
            OnMoveLane = null;
            OnGetParent = null;
        }

        public event TrafficEventHandler OnMoveLane;
        public void MoveLane()
        {
            if (OnMoveLane != null)
                OnMoveLane(this, new TrafficEventArgs { car = (ICarObject)this });
        }

        public event TrafficEventHandler OnDeadEnd;
        public void DeadEnd(TrafficEventArgs e)
        {
            if (OnDeadEnd != null)
                OnDeadEnd(this, e);
        }
        public event TrafficEventHandler OnGetParent;        
        public ILaneObject GetParent()
        {
            if (OnGetParent != null)
            {
                TrafficEventArgs args;
                args = new TrafficEventArgs{ car = (ICarObject)this, lane = null };
                OnGetParent(this, args);
                return args.lane;
            }
            return null;
        }
    }
}
